// import App from '@/App';
import VueRouter from 'vue-router';
import Vue from 'vue';
import login from '../pages/login/login.vue';
import dashboard from '../pages/dashboard/index.vue';
import Layout from '../layout/Layout.vue';

Vue.use(VueRouter);
export const routes = [{
  path: '/',
  component: Layout,
  redirect: '/dashboard',
  children: [{
    name: 'dashboard',
    path: '/dashboard',
    meta: {
      name: '仪表盘',
      icon: 'dashboard',
    },
    component: dashboard,
  }],
},
{
  path: '/tree',
  component: Layout,
  children: [{
    name: 'tree',
    path: '',
    component: () => import('../pages/tree/index.vue'),
    meta: {
      name: '嵌套结构',
      icon: 'tree',
      link: false,
    },
    redirect: '/tree/sub1',
    children: [{
      name: 'sub1',
      path: 'sub1',
      meta: {
        name: 'sub1',
        icon: '',
      },
      component: () => import('../pages/tree/sub1.vue'),
      // redirect: '/tree/sub1/sub1-1',
      children: [{
        name: 'sub1-1',
        path: 'sub1-1',
        meta: {
          name: 'sub1-1',
          icon: '',
        },
        // redirect: '/tree/sub1/sub1-1/sub1-1-1',
        component: () => import('../pages/tree/sub1/sub1-1.vue'),
        children: [{
          name: 'sub1-1-1',
          path: 'sub1-1-1',
          component: () => import('../pages/tree/sub1/sub1-1/sub1-1-1.vue'),
          meta: {
            name: 'sub1-1-1',
            icon: '',
          },
        },
        {
          name: 'sub1-1-2',
          path: 'sub1-1-2',
          component: () => import('../pages/tree/sub1/sub1-1/sub1-1-2.vue'),
          meta: {
            name: 'sub1-1-2',
            icon: '',
          },
        },
        ],
      },
      {
        name: 'sub1-2',
        path: 'sub1-2',
        component: () => import('../pages/tree/sub1/sub1-2.vue'),
        meta: {
          name: 'sub1-2',
          icon: '',
        },
        children: [{
          name: 'sub1-2-1',
          path: 'sub1-2-1',
          component: () => import('../pages/tree/sub1/sub1-2/sub1-2-1.vue'),
          meta: {
            name: 'sub1-2-1',
            icon: '',
          },
        },
        {
          name: 'sub1-2-2',
          path: 'sub1-2-2',
          component: () => import('../pages/tree/sub1/sub1-2/sub1-2-2.vue'),
          meta: {
            name: 'sub1-2-2',
            icon: '',
          },
        },
        ],
      },
      ],
    }],
  },

  ],
},
{
  path: '/table',
  component: Layout,
  children: [{
    name: 'table',
    path: '',
    component: () => import('../pages/table/index.vue'),
    meta: {
      name: 'table',
      icon: 'dashboard',
    },
  }],
},
{
  name: 'login',
  path: '/login',
  meta: {
    name: 'login',
    icon: '',
    hidden: true,
  },
  component: login,
},
];

// 动态路由
export const asyncRoutes = [
  {
    path: '/permission',
    component: Layout,
    meta: { roles: ['admin'] },
    children: [
      {
        name: 'userPermission',
        path: '',
        component: () => import('../async_pages/permission/index.vue'),
        meta: { name: '用户权限管理', icon: 'tree', roles: ['admin', 'superadmin'] },
      },
    ],
  },
];

const router = new VueRouter({
  routes: [...routes],
});
export function resetRouter() {
  // 防止将asyncRoute添加到原routes中，导致每次（不刷新）退出
  // 再登陆时，使routes越来越大
  router.options.routes = [...routes];
  // 在调用router.addRoutes后，重置router
  router.matcher = (new VueRouter({ routes })).matcher;
}

export default router;
